package org.dromara.yuanshidian.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.yuanshidian.domain.bo.Ab04RefundBo;
import org.dromara.yuanshidian.domain.vo.Ab04RefundVo;
import org.dromara.yuanshidian.domain.Ab04Refund;
import org.dromara.yuanshidian.mapper.Ab04RefundMapper;
import org.dromara.yuanshidian.service.IAb04RefundService;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 退费计息明细Service业务层处理
 *
 * @author tzhong
 * @date 2025-06-16
 */
@RequiredArgsConstructor
@Service
public class Ab04RefundServiceImpl implements IAb04RefundService {

    private final Ab04RefundMapper baseMapper;

    /**
     * 查询退费计息明细
     *
     * @param id 主键
     * @return 退费计息明细
     */
    @Override
    public Ab04RefundVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询退费计息明细列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 退费计息明细分页列表
     */
    @Override
    public TableDataInfo<Ab04RefundVo> queryPageList(Ab04RefundBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Ab04Refund> lqw = buildQueryWrapper(bo);
        Page<Ab04RefundVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的退费计息明细列表
     *
     * @param bo 查询条件
     * @return 退费计息明细列表
     */
    @Override
    public List<Ab04RefundVo> queryList(Ab04RefundBo bo) {
        LambdaQueryWrapper<Ab04Refund> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Ab04Refund> buildQueryWrapper(Ab04RefundBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Ab04Refund> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getAccountingRate() != null, Ab04Refund::getAccountingRate, bo.getAccountingRate());
        lqw.eq(bo.getMonthlyPayment() != null, Ab04Refund::getMonthlyPayment, bo.getMonthlyPayment());
        lqw.eq(bo.getAnnualPayment() != null, Ab04Refund::getAnnualPayment, bo.getAnnualPayment());
        lqw.eq(bo.getLastYearAmount() != null, Ab04Refund::getLastYearAmount, bo.getLastYearAmount());
        lqw.eq(bo.getLastYearAccumulateAmount() != null, Ab04Refund::getLastYearAccumulateAmount, bo.getLastYearAccumulateAmount());
        lqw.eq(bo.getCurrentYearInterestIncome() != null, Ab04Refund::getCurrentYearInterestIncome, bo.getCurrentYearInterestIncome());
        lqw.eq(bo.getInterestIncome() != null, Ab04Refund::getInterestIncome, bo.getInterestIncome());
        lqw.eq(StringUtils.isNotBlank(bo.getUserCard()), Ab04Refund::getUserCard, bo.getUserCard());
        return lqw;
    }

    /**
     * 新增退费计息明细
     *
     * @param bo 退费计息明细
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(Ab04RefundBo bo) {
        Ab04Refund add = MapstructUtils.convert(bo, Ab04Refund.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改退费计息明细
     *
     * @param bo 退费计息明细
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(Ab04RefundBo bo) {
        Ab04Refund update = MapstructUtils.convert(bo, Ab04Refund.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Ab04Refund entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除退费计息明细信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public BigDecimal getGrjfbxhByuserCard(String userCard) {

        return baseMapper.getGrjfbxhByuserCard(userCard);
    }
}
